1811F - Is It Flower - CodeForces Solution


dfs and similar graphs implementation *2100

Please click on ads to support us..

C++ Code:

#include <bits/stdc++.h>
using namespace std;
vector <int> Con[200001];
int odwiedzone[200001];
void DFS(int a){
    odwiedzone[a] = 1;
    for (int i = 0; i < Con[a].size();i++){
        if(odwiedzone[Con[a][i]] == 0){
            DFS(Con[a][i]);
        }
    }
}
int DFS2(int a,int ojciec,int przodek){
    for (int i = 0; i < Con[a].size();i++){
        if(Con[a][i] != ojciec){
            if(Con[Con[a][i]].size() == 4){
                if(Con[a][i] != przodek)return 10000000;
                return 2;
            }
            else{
                return DFS2(Con[a][i],a,przodek) + 1;
            }
        }
    }
}

void solve(){
    int n,m;
    cin >> n >> m;
    for (int i = 1;i <= n;i++){
        Con[i].clear();
        odwiedzone[i] = 0;
    }
    for (int i = 1; i <= m;i++){
        int temp1,temp2;
        cin >> temp1 >> temp2;
        Con[temp1].push_back(temp2);
        Con[temp2].push_back(temp1);
    }
    long long licznik4 = 0;
    long long licznik2 = 0;
    bool is_flower = 1;
    for (int i = 1; i <= n;i++){
        if(Con[i].size() == 4){
            licznik4++;
        }
        if(Con[i].size() == 2){
            licznik2++;
        }
        if(Con[i].size() != 2 &&Con[i].size() != 4){
            is_flower = 0;
        }
    }
    //czy jest dobrze krawedzi
    if(licznik4 * licznik4 != licznik4 + licznik2){
        is_flower = 0;
    }
    //czy jest polaczony
    DFS(1);
    for (int i = 1;i <= n;i++){
        if(odwiedzone[i] == 0)is_flower = 0;
    }
    if(is_flower == 1){
        for (int i = 1; i <= n;i++){
            if(Con[i].size() == 4){
                int j = 0;
                while (j < Con[i].size() && Con[Con[i][j]].size() != 2){
                    j++;
                }
                if(j == Con[i].size())is_flower = 0;
                if( DFS2(Con[i][j],i,i) != licznik4)is_flower = 0;
            }
        }
        if(is_flower == 1){
            cout << "YES";
        }
        else{
            cout << "NO";
        }

    }
    else{
        cout << "NO";
    }

}
int main(){
    int q;
    cin >> q;
    while (q > 0){
        q--;
        solve();
        cout << '\n';
    }
}


Comments

Submit
0 Comments
More Questions

507B - Amr and Pins
379A - New Year Candles
1154A - Restoring Three Numbers
750A - New Year and Hurry
705A - Hulk
492B - Vanya and Lanterns
1374C - Move Brackets
1476A - K-divisible Sum
1333A - Little Artem
432D - Prefixes and Suffixes
486A - Calculating Function
1373B - 01 Game
1187A - Stickers and Toys
313B - Ilya and Queries
579A - Raising Bacteria
723A - The New Year Meeting Friends
302A - Eugeny and Array
1638B - Odd Swap Sort
1370C - Number Game
1206B - Make Product Equal One
131A - cAPS lOCK
1635A - Min Or Sum
474A - Keyboard
1343A - Candies
1343C - Alternating Subsequence
1325A - EhAb AnD gCd
746A - Compote
318A - Even Odds
550B - Preparing Olympiad
939B - Hamster Farm